import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sports_new/base/base_controller.dart';
import 'package:sports_new/controller/expert/expert_divide_logic.dart';
import 'package:sports_new/controller/expert/expert_top_logic.dart';
import 'package:sports_new/controller/expert/plan_form_logic.dart';
import 'package:sports_new/controller/navigation_logic.dart';
import 'package:sports_new/controller/vip/vip_logic.dart';
import 'package:sports_new/utils/mode_config.dart';
import 'package:sports_new/view/overlay/ads_float_widget.dart';
import 'package:sports_new/view/expert/expert_choice_page.dart';
import 'package:sports_new/view/expert/plan_list/plan_form_page.dart';
import 'package:sports_new/view/expert/top/expert_top_page.dart';
import '../../utils/routes.dart';
import 'expert_page_logic.dart';

class PlanLogic extends BaseController with GetTickerProviderStateMixin {
  List<String> routes = [
    Routes.homeExpertTop,
    Routes.expert,
    if (ModeConfig.planForm) Routes.planForm
  ];
  List<Widget> tabView = [
    const ExpertTopPage(showAppBar: false),
    const ExpertDividePage(),
    if (ModeConfig.planForm) const PlanFormPage()
  ];
  List<String> tabs = ["top", "expert", if (ModeConfig.planForm) "plan_form"];
  List<String> tabName = ["专家榜", "彩经", if (ModeConfig.planForm) "计划单"];
  late TabController tabController =
      TabController(length: tabs.length, vsync: this, initialIndex: _tabIndex);
  int _tabIndex = 1;

  String get currentRoute {
    switch (routes[tabIndex]) {
      default:
        return routes[tabIndex];
    }
  }

  int get tabIndex => _tabIndex;

  set tabIndex(int value) {
    _tabIndex = value;
    update();
  }

  @override
  void onInit() {
    tabController.addListener(() {
      tabIndex = tabController.index;
      Get.find<NavigationLogic>().getRoute();
      AdsFloatWidget.checkAds(routes[tabIndex]);
    });
    super.onInit();
  }

  Future doRefresh() async {
    switch (routes[tabIndex]) {
      case Routes.expert:
        Get.find<ExpertDivideLogic>().doRefresh();
        break;
      case Routes.homeExpertTop:
        Get.find<ExpertTopLogic>().doRefresh();
        break;
      case Routes.planForm:
        Get.find<PlanFormLogic>().doRefresh();
        break;
      case Routes.vipCenter:
        Get.find<VipLogic>(tag: "false").doRefresh();
    }
  }

  doJump(
    String route, {
    dynamic arguments,
    Map<String, String>? parameters,
  }) {
    switch (route) {
      case Routes.expert:
        tabController.animateTo(1);
        break;
      case Routes.homeExpertTop:
        tabController.animateTo(2);
        break;
      case Routes.planForm:
        tabController.animateTo(3);
        break;
      default:
        Get.toNamed(route, arguments: arguments, parameters: parameters);
    }
  }

  toIndex(int index) {
    tabController.animateTo(index);
  }

  @override
  void getRefresh() {
    try {
      Get.find<ExpertPageLogic>().getRefresh();
    } catch (e) {
      log("ExpertPageLogic not start");
    }
    try {
      Get.find<PlanFormLogic>().getRefresh();
    } catch (e) {
      log("PlanFormLogic not start");
    }
    try {
      Get.find<ExpertTopLogic>().getRefresh();
    } catch (e) {
      log("ExpertTopLogic not start");
    }
    update();
    super.getRefresh();
  }
}
